﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ZedGraph;
using AntikCompta.Data;

namespace AntikCompta
{
    public partial class GraphWnd : Form
    {
        private DataMng mDatas;
        private Stock mStock;
        internal GraphWnd()
        {
            InitializeComponent();
        }

        internal GraphWnd(DataMng aDatas, Stock aStock)
        {
            InitializeComponent();
            mDatas = aDatas;
            mStock = aStock;
        }

        private void bt_fermer_Click(object sender, EventArgs e)
        {
            Close();
        }

        // Call this method from the Form_Load method, passing your ZedGraphControl
        virtual public void CreateChart(ZedGraphControl zgc)
        {
            MasterPane myMaster = zgc.MasterPane;

            myMaster.PaneList.Clear();

            // Set the masterpane title
            myMaster.Title.Text = "Evolutions par années";
            myMaster.Title.IsVisible = true;

            // Fill the masterpane background with a color gradient
            myMaster.Fill = new Fill(Color.White, Color.MediumSlateBlue, 45.0F);

            // Set the margins to 10 points
            myMaster.Margin.All = 10;

            // Enable the masterpane legend
            myMaster.Legend.IsVisible = true;
            myMaster.Legend.Position = LegendPos.TopCenter;

            AfficheErreurs(myMaster);

            // Initialize a color and symbol type rotator
            ColorSymbolRotator rotator = new ColorSymbolRotator();

            AddAchats(myMaster, rotator);
            AddVentes(myMaster, rotator);
            AddStock(myMaster, rotator);
            AddFrais(myMaster, rotator);
            AddCaisse(myMaster, rotator);
            AddBenefice(myMaster, rotator);

            using (Graphics g = this.CreateGraphics())
            {
                // Tell ZedGraph to auto layout the new GraphPanes
                myMaster.SetLayout(g, PaneLayout.SquareColPreferred);
            }

            zgc.AxisChange();
        }

        private void AfficheErreurs(MasterPane myMaster)
        {
            // Add a priority stamp
            if (!mDatas.Journal.HasError)
                return;

            TextObj text = new TextObj("ERREURS\n(voir le journal)", 0.5F, 0.5F);
            text.Location.CoordinateFrame = CoordType.PaneFraction;
            text.FontSpec.Angle = 30.0F;
            text.FontSpec.FontColor = Color.Red;
            text.FontSpec.IsBold = true;
            text.FontSpec.Size = 50;
            text.FontSpec.Border.IsVisible = false;
            text.FontSpec.Border.Color = Color.Red;
            text.FontSpec.Fill.IsVisible = false;
            text.Location.AlignH = AlignH.Center;
            text.Location.AlignV = AlignV.Center;
            text.ZOrder = ZOrder.A_InFront;
            myMaster.GraphObjList.Add(text);

        }

        private void AddBenefice(MasterPane myMaster, ColorSymbolRotator rotator)
        {
            // Create a new graph - rect dimensions do not matter here, since it
            // will be resized by MasterPane.AutoPaneLayout()
            GraphPane myPane = new GraphPane(new Rectangle(10, 10, 10, 10),
               "Bénéfices",
               "Années",
               "Valeur en €");

            // Fill the GraphPane background with a color gradient
            myPane.Fill = new Fill(Color.White, Color.LightYellow, 45.0F);
            myPane.BaseDimension = 6.0F;

            // Make up some data arrays based on the Sine function
            PointPairList list = new PointPairList();
            foreach (Exercice ex in mDatas.ExerciceListe)
            {
                double x = (double)ex.Annee;
                double y = Math.Round(mDatas.GetBenefice(ex.Annee), 2);
                list.Add(x, y);
            }

            // Add a curve to the Graph, use the next sequential color and symbol
            LineItem myCurve = myPane.AddCurve("Bénéfices",
               list, rotator.NextColor, rotator.NextSymbol);
            // Fill the symbols with white to make them opaque
            myCurve.Symbol.Fill = new Fill(Color.White);

            // Add the GraphPane to the MasterPane
            myMaster.Add(myPane);
        }

        private void AddCaisse(MasterPane myMaster, ColorSymbolRotator rotator)
        {
            // Create a new graph - rect dimensions do not matter here, since it
            // will be resized by MasterPane.AutoPaneLayout()
            GraphPane myPane = new GraphPane(new Rectangle(10, 10, 10, 10),
               "Caisses",
               "Années",
               "Valeur en €");

            // Fill the GraphPane background with a color gradient
            myPane.Fill = new Fill(Color.White, Color.LightYellow, 45.0F);
            myPane.BaseDimension = 6.0F;

            // Make up some data arrays based on the Sine function
            PointPairList list = new PointPairList();
            foreach(Exercice ex in mDatas.ExerciceListe)
            {
                double x = ex.Annee;
                
                DateTime date = new DateTime(ex.Annee, 12, 31);
                double y = Math.Round(mDatas.GetResteCaisse(ref mStock, date), 2);
                list.Add(x, y);
            }

            // Add a curve to the Graph, use the next sequential color and symbol
            LineItem myCurve = myPane.AddCurve("Caisses",
               list, rotator.NextColor, rotator.NextSymbol);
            // Fill the symbols with white to make them opaque
            myCurve.Symbol.Fill = new Fill(Color.White);

            // Add the GraphPane to the MasterPane
            myMaster.Add(myPane);
        }

        private void AddFrais(MasterPane myMaster, ColorSymbolRotator rotator)
        {
            // Create a new graph - rect dimensions do not matter here, since it
            // will be resized by MasterPane.AutoPaneLayout()
            GraphPane myPane = new GraphPane(new Rectangle(10, 10, 10, 10),
               "Frais",
               "Années",
               "Valeur en €");

            // Fill the GraphPane background with a color gradient
            myPane.Fill = new Fill(Color.White, Color.LightYellow, 45.0F);
            myPane.BaseDimension = 6.0F;

            // Make up some data arrays based on the Sine function
            PointPairList list = new PointPairList();
            foreach (Exercice ex in mDatas.ExerciceListe)
            {
                double x = (double)ex.Annee;
                float somme = 0;
                foreach (CaisseElement elmt in ex.Frais)
                {
                    somme += elmt.Somme;
                }
                double y = Math.Round(somme, 2);
                list.Add(x, y);
            }

            // Add a curve to the Graph, use the next sequential color and symbol
            LineItem myCurve = myPane.AddCurve("Frais",
               list, rotator.NextColor, rotator.NextSymbol);
            // Fill the symbols with white to make them opaque
            myCurve.Symbol.Fill = new Fill(Color.White);

            // Add the GraphPane to the MasterPane
            myMaster.Add(myPane);
        }

        private void AddStock(MasterPane myMaster, ColorSymbolRotator rotator)
        {
            // Create a new graph - rect dimensions do not matter here, since it
            // will be resized by MasterPane.AutoPaneLayout()
            GraphPane myPane = new GraphPane(new Rectangle(10, 10, 10, 10),
               "Stocks",
               "Années",
               "Valeur en €");

            // Fill the GraphPane background with a color gradient
            myPane.Fill = new Fill(Color.White, Color.LightYellow, 45.0F);
            myPane.BaseDimension = 6.0F;

            // Make up some data arrays based on the Sine function
            PointPairList list = new PointPairList();
            foreach (Exercice ex in mDatas.ExerciceListe)
            {
                double x = (double)ex.Annee;
                double y = Math.Round(mDatas.GetValeurStockFinal(ex.Annee), 2);
                list.Add(x, y);
            }

            // Add a curve to the Graph, use the next sequential color and symbol
            LineItem myCurve = myPane.AddCurve("Stocks",
               list, rotator.NextColor, rotator.NextSymbol);
            // Fill the symbols with white to make them opaque
            myCurve.Symbol.Fill = new Fill(Color.White);

            // Add the GraphPane to the MasterPane
            myMaster.Add(myPane);
        }

        private void AddVentes(MasterPane myMaster, ColorSymbolRotator rotator)
        {
            // Create a new graph - rect dimensions do not matter here, since it
            // will be resized by MasterPane.AutoPaneLayout()
            GraphPane myPane = new GraphPane(new Rectangle(10, 10, 10, 10),
               "Ventes",
               "Années",
               "Valeur en €");

            // Fill the GraphPane background with a color gradient
            myPane.Fill = new Fill(Color.White, Color.LightYellow, 45.0F);
            myPane.BaseDimension = 6.0F;

            // Make up some data arrays based on the Sine function
            PointPairList list = new PointPairList();
            foreach (Exercice ex in mDatas.ExerciceListe)
            {
                double x = (double)ex.Annee;
                double y = Math.Round(mDatas.GetVentes(ex.Annee), 2);
                list.Add(x, y);
            }

            // Add a curve to the Graph, use the next sequential color and symbol
            LineItem myCurve = myPane.AddCurve("Ventes",
               list, rotator.NextColor, rotator.NextSymbol);
            // Fill the symbols with white to make them opaque
            myCurve.Symbol.Fill = new Fill(Color.White);

            // Add the GraphPane to the MasterPane
            myMaster.Add(myPane);
        }

        private void AddAchats(MasterPane myMaster, ColorSymbolRotator rotator)
        {
            // Create a new graph - rect dimensions do not matter here, since it
            // will be resized by MasterPane.AutoPaneLayout()
            GraphPane myPane = new GraphPane(new Rectangle(10, 10, 10, 10),
               "Achats",
               "Années",
               "Valeur en €");

            // Fill the GraphPane background with a color gradient
            myPane.Fill = new Fill(Color.White, Color.LightYellow, 45.0F);
            myPane.BaseDimension = 6.0F;

            // Make up some data arrays based on the Sine function
            PointPairList list = new PointPairList();
            foreach (Exercice ex in mDatas.ExerciceListe)
            {
                double x = (double)ex.Annee;
                double y = Math.Round(mDatas.GetAchats(ex.Annee), 2);
                list.Add(x, y);
            }

            // Add a curve to the Graph, use the next sequential color and symbol
            LineItem myCurve = myPane.AddCurve("Achats",
               list, rotator.NextColor, rotator.NextSymbol);
            // Fill the symbols with white to make them opaque
            myCurve.Symbol.Fill = new Fill(Color.White);

            // Add the GraphPane to the MasterPane
            myMaster.Add(myPane);
        }

        private void GraphWnd_Load(object sender, EventArgs e)
        {
            CreateChart(graph);
        }

        private bool graph_DoubleClickEvent(ZedGraphControl sender, MouseEventArgs e)
        {
            GraphPane gp = sender.MasterPane.FindPane(e.Location);
            if (gp != null)
            {
                GraphZoomWnd zoom = new GraphZoomWnd(gp.Clone());
                zoom.ShowDialog(this);
            }

            return default(bool);
        }
    }

    public class GraphZoomWnd : GraphWnd
    {
        private GraphPane mGp;
        internal GraphZoomWnd(GraphPane aGp)
        {
            mGp = aGp;
        }

        public override void CreateChart(ZedGraphControl zgc)
        {
            MasterPane myMaster = zgc.MasterPane;

            myMaster.PaneList.Clear();

            // Set the masterpane title
            myMaster.Title.Text = "Zoom";
            myMaster.Title.IsVisible = true;

            // Fill the masterpane background with a color gradient
            myMaster.Fill = new Fill(Color.White, Color.MediumSlateBlue, 45.0F);

            // Set the margins to 10 points
            myMaster.Margin.All = 10;

            // Enable the masterpane legend
            myMaster.Legend.IsVisible = true;
            myMaster.Legend.Position = LegendPos.TopCenter;

            myMaster.Add(mGp);

            using (Graphics g = this.CreateGraphics())
            {
                // Tell ZedGraph to auto layout the new GraphPanes
                myMaster.SetLayout(g, PaneLayout.ForceSquare);
            }

            zgc.AxisChange();
        }
    }
}
